package com.gmail.volodymyrdotsenko.roo.webeasy;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.springframework.roo.model.JavaPackage;
import org.springframework.roo.model.JavaType;
import org.springframework.roo.project.GAV;
import org.springframework.roo.project.packaging.JarPackaging;
import org.springframework.roo.project.packaging.PackagingProvider;
import org.springframework.roo.shell.CliAvailabilityIndicator;
import org.springframework.roo.shell.CliCommand;
import org.springframework.roo.shell.CliOption;
import org.springframework.roo.shell.CommandMarker;

import com.gmail.volodymyrdotsenko.roo.webeasy.packaging.WebeasyPackagingProvider;

/**
 * Sample of a command class. The command class is registered by the Roo shell
 * following an automatic classpath scan. You can provide simple user
 * presentation-related logic in this class. You can return any objects from
 * each method, or use the logger directly if you'd like to emit messages of
 * different severity (and therefore different colours on non-Windows systems).
 * 
 * @since 1.1
 */
@Component
// Use these Apache Felix annotations to register your commands class in the Roo
// container
@Service
public class WebeasyCommands implements CommandMarker { // All command types
														// must implement the
														// CommandMarker
														// interface

	/**
	 * Get a reference to the WebeasyOperations from the underlying OSGi
	 * container
	 */
	@Reference
	private WebeasyOperations operations;

	/**
	 * This method is optional. It allows automatic command hiding in situations
	 * when the command should not be visible. For example the 'entity' command
	 * will not be made available before the user has defined his persistence
	 * settings in the Roo shell or directly in the project.
	 * 
	 * You can define multiple methods annotated with
	 * {@link CliAvailabilityIndicator} if your commands have differing
	 * visibility requirements.
	 * 
	 * @return true (default) if the command should be visible at this stage,
	 *         false otherwise
	 */
	@CliAvailabilityIndicator({ "webeasy add" })
	public boolean isCommandAvailable() {
		return operations.isCommandAvailable();
	}

	/**
	 * This method registers a command with the Roo shell. It also offers a
	 * mandatory command attribute.
	 * 
	 * @param type
	 */
	@CliCommand(value = "webeasy add", help = "Some helpful description")
	public void add(
			@CliOption(key = "type", mandatory = true, help = "The java type to apply this annotation to") JavaType target) {
		// operations.annotateType(target);
	}

	/**
	 * This method registers a command with the Roo shell. It has no command
	 * attribute.
	 * 
	 */
	@CliCommand(value = "webeasy project", help = "Create web spring mvc project")
	public void createProject(
			@CliOption(key = { "", "topLevelPackage" }, mandatory = true, optionContext = "update", help = "The uppermost package name (this becomes the <groupId> in Maven and also the '~' value when using Roo's shell)") final JavaPackage topLevelPackage,
			@CliOption(key = "projectName", help = "The name of the project (last segment of package name used as default)") final String projectName,
			@CliOption(key = "java", help = "Forces a particular major version of Java to be used (will be auto-detected if unspecified; specify 5 or 6 or 7 only)") final Integer majorJavaVersion,
			@CliOption(key = "parent", help = "The Maven coordinates of the parent POM, in the form \"groupId:artifactId:version\"") final GAV parentPom,
			@CliOption(key = "packaging", help = "The Maven packaging of this project", unspecifiedDefaultValue = WebeasyPackagingProvider.NAME) final PackagingProvider packaging) {

		operations.createProject(topLevelPackage, projectName,
				majorJavaVersion, parentPom, packaging);
	}
}